/**
 * @file /include/weeding_robot_gui/main_window.hpp
 *
 * @brief Qt based gui for weeding_robot_gui.
 *
 * @date 2021.7.21
 **/
#ifndef weeding_robot_gui_MAIN_WINDOW_H
#define weeding_robot_gui_MAIN_WINDOW_H

/*****************************************************************************
** Includes
*****************************************************************************/

//#include <QtGui/QMainWindow>
#include <QProcess>
#include <QTreeWidgetItem>
#include <QStandardItemModel>
#include <QComboBox>
#include <QSpinBox>
#include <QTextCodec>
#include <QVariant>
#include <map>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QString>
#include <QByteArray>
#include <QTimer>
#include <QTextStream>


#include "ui_main_window.h"
#include "qnode.hpp"
#include "settings.h"
#include "rocker.h"
#include "CCtrlDashBoard.h"
/*****************************************************************************
** Namespace
*****************************************************************************/

namespace weeding_robot_gui {

/*****************************************************************************
** Interface [MainWindow]
*****************************************************************************/
/**
 * @brief Qt central, all operations relating to the view part here.
 */
class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(int argc, char** argv, QWidget *parent = 0);
    Ui::MainWindowDesign ui;
    ~MainWindow();
    enum {upleft=0,up,upright,left,stop,right,downleft,down,downright};
    void ReadSettings(); // Load up qt program settings at startup
    void WriteSettings(); // Save qt program settings when closing

    void closeEvent(QCloseEvent *event); // Overloaded function
    void showNoMasterMessage();

    void connections();
    void initVideos();
    void initSerialPort();
    void serial_port_send_motion_control_message(int right_wheel_velocity,int left_wheel_velocity);
    void serial_port_send_actuator_control_message(QByteArray send_message);
public Q_SLOTS:
    /******************************************
    ** Auto-connections (connectSlotsByName())
    *******************************************/
    void on_actionAbout_triggered();
    void on_button_connect_clicked(bool check );
    void on_checkbox_use_environment_stateChanged(int state);

    /******************************************
    ** Manual connections
    *******************************************/
    void updateLoggingView(); // no idea why this can't connect automatically
    void initUI();

    void slot_settingsFrame();
    void slot_showVideo(int,QImage);
    void slot_robot_rocker(int);
    void slot_checkBox_robot_control_used(int state);
    void slot_checkBox_camera_control_used(int state);
    void slot_checkBox_actuator_control_used(int state);
    void slot_robot_pushButton_control();
    void slot_serial_port_open_read();
    void slot_serial_port_send_message();
    void slot_serial_port_read_message();
    void slot_serial_port_show_message();
    void slot_actuator_pushButton_control();
    void slot_serial_port_save_current_text();
    void slot_robot_pushButton_send_message_control();
    void slot_robot_pushButton_steering_stop();
    void slot_robot_pushButton_control_EX();
    void slot_robot_pushButton_control_MG();
private slots:
    void on_pushButton_roscore_clicked();
    void on_readCMDOutput();
    void on_readCMDError();

    void on_pushButton_CMDSend_clicked();

    void on_pushButton_OpenCamera_clicked();

private:
    QNode qnode;
    bool is_ros_connected;

    QString master_url;
    QString host_url;
    bool use_enviroment_variables = false;
    bool remember_settings = true;
    bool robot_control_enabled = false;
    bool actuator_control_enabled = false;
    bool camera_control_enabled;

    int g_x, g_y, g_z;

    settings* setting = NULL;
    rocker *robot_rocker;
    rocker *actuator_rocker;
    CCtrlDashBoard *linear_velocity_display_dashboard;
    CCtrlDashBoard *angle_velocity_display_dashboard;
    QSerialPort *current_serial_port;
    QByteArray showdata;
    QTimer *timerSerial;
    QProcess* cmd;
    QByteArray buffer;
};

}  // namespace weeding_robot_gui

#endif // weeding_robot_gui_MAIN_WINDOW_H
